package com.example.aigc_education.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.aigc_education.domain.CozeTask;
import com.example.aigc_education.integration.emuns.CozeTaskStatus;
import com.example.aigc_education.integration.threeparty.CozeService;
import com.example.aigc_education.mapper.CozeTaskMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Component
@EnableScheduling
public class CozeTaskCheckJob {

    @Resource
    private CozeService cozeService;

    @Resource
    private CozeTaskMapper cozeTaskMapper;

    /**
     * 查询异步任务结果
     * 每15s轮询一次
     */
    @Async("taskExecutor")
    @Scheduled(cron = "0/15 * * * * ?")
    public void queryAsyncTaskResult() {
        // 查询当前有running任务的计划
        List<Long> runningPlanIds = cozeTaskMapper.queryPlanIdsByTaskStatus(CozeTaskStatus.RUNNING.getValue());
        runningPlanIds.forEach(runningPlanId -> {
            cozeService.queryAsyncTaskResult(runningPlanId);
        });
    }
}
